package com.buyantech.smweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;
import javacommon.util.DateTimeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.model.Suggestion;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.SuggestionManager;
import com.buyantech.smweb.vo.query.SuggestionQuery;
import com.google.gson.Gson;

/**
 * 意见反馈
 * @version 1.0
 * @date 2012-05-10
 */

@Controller
public class SuggestionController extends BaseSpringController {

	private SuggestionManager suggestionManager;
	private final String LIST_ACTION = "redirect:/pages/User/list.do";

	public void setSuggestionManager(SuggestionManager suggestionManager) {
		this.suggestionManager = suggestionManager;
	}

	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}
	
	
	/** 
	 * 执行搜索 
	 **/
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,SuggestionQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex"))+1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile=request.getParameter("sortField");
		if(sortfile!=null&&!sortfile.equals(""))
		  query.setSortColumns(request.getParameter("sortField")+" "+request.getParameter("sortOrder"));
		Page page = this.suggestionManager.searchPage(query);
		List<Suggestion> list=page.getResult();
		for(Suggestion suggestion:list){
			suggestion.setCrdate(suggestion.getCrdate().substring(0,16));
		}
		modelMap.put("data", page.getResult());
		modelMap.put("total",page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}	
	
	/**
	 * 登出
	 **/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		String id=request.getParameter("id");
		if(id.length()>0){
			id=id.substring(0,id.length()-1);
		}
		try {
			suggestionManager.remove("DELETE FROM User WHERE id in("+id+")");
			ajaxPack("{state:true}",request,response);
		} catch (RuntimeException e) {
			ajaxPack("{state:false}",request,response);
		}
		return null;
	}
	
	/**
	 * 根据id获取用户对象
	 **/
	public ModelAndView getId(HttpServletRequest request,
			HttpServletResponse response,Suggestion suggestion) throws Exception {
		String id=request.getParameter("id");
		suggestion=suggestionManager.getById(id);
		if(suggestion!=null){
			JSONObject obj = new JSONObject(suggestion);
			ajaxPack(obj.toString(), request, response);
		}
		return null;
	}
	
	/** 
	 * 保存新增对象
	 **/
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,Suggestion suggestion) throws Exception {
		String json=request.getParameter("submitData");
		Gson gson=new Gson();
		Map<String,String> map = new HashMap<String,String>();
		map = gson.fromJson(json,Map.class);
		try {
			suggestion=suggestionManager.getById(map.get("id"));
			suggestion.setContent2(map.get("remark"));
			suggestion.setHudate(DateTimeUtil.getDateTimeString3());
			suggestionManager.update(suggestion);
		}catch (RuntimeException e) {
			ajaxPack("{state:false}",request,response);
			return null;
		}
		ajaxPack("{state:true}",request,response);
		return null;
	}
 
	
}
